package com.fzj.example.design.proxy.proxydynamic.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
* @Description
* @Author  fanzj
* @Date   2020/5/8 6:03 下午
* @Param  
* @Return      
* @Exception   
* 
*/
public class UserServiceCglib implements MethodInterceptor {

    private Object target;

    /**
     * 创建代理实例
     * @param target
     * @return
     */
    public Object getInstance(Object target){
        this.target = target;
        Enhancer enhancer =new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("事务开始。。。");
        Object result = methodProxy.invokeSuper(o, args);
        System.out.println("事务结束。。。");
        return result;
    }
    
}
